import { formatComment } from '@/components/issues-answers/format';

const formatVoteStates = (issue) => {
  if (!issue.has_vote) return {};
  return {
    collect: issue.has_vote.collection,
    like: issue.has_vote.like,
    dislike: issue.has_vote.dis_like,
  };
};

const formatVoteCounts = issue => ({
  like: issue.votes.like_size,
  dislike: issue.votes.dis_like_size,
});

export const formatIssue = issue => ({
  id: issue.id,
  title: issue.title,
  content: issue.content,
  tags: issue.tags,
  good: issue.essence,
  solved: issue.is_solve,

  author: issue.owner, // id nickname avatar

  wxShare: issue.share_url, // 微信小程序二维码图片地址
  acceptedComment: issue.comment ? formatComment(issue.comment) : {},

  commentsCount: issue.comment_count,
  discussionsCount: issue.discuss_count,

  voteStates: formatVoteStates(issue),
  voteCounts: formatVoteCounts(issue),

  space: { // 所属空间
    id: issue.product_id,
    title: issue.product_title,
  },

  created_at: issue.created_at,
  updated_at: issue.updated_at,
});

export const formatIssues = issues => issues.map(i => formatIssue(i));
